package bancosys.tec.security.impl.services.incluir;

import jmine.tec.di.annotation.Injected;
import jmine.tec.services.annotations.Execution;
import jmine.tec.services.annotations.Input;
import jmine.tec.services.annotations.ServiceImplementor;
import jmine.tec.services.api.ActionsEnum;
import bancosys.tec.persist.dao.BaseDAO;
import bancosys.tec.persist.dao.BeanNotFoundException;
import bancosys.tec.security.impl.domain.SecurityClient;

/**
 * Serviço de inclusão da entidade {@link SecurityClient}.
 * 
 * @author Rafael Volpato
 * @date Oct 19, 2010
 */
@ServiceImplementor(action = ActionsEnum.INCLUIR)
public class SecurityClientService {

    public static final String DATABASE_ID = "Database ID";

    public static final String NOME = "Nome";

    private BaseDAO<SecurityClient> dao;

    @Input(fieldName = DATABASE_ID)
    private Long databaseID;

    @Input(fieldName = NOME)
    private String nome;

    /**
     * @param dao security client DAO
     */
    @Injected
    public void setDao(BaseDAO<SecurityClient> dao) {
        this.dao = dao;
    }

    /**
     * Inclui um novo {@link SecurityClient}.
     */
    @SuppressWarnings("deprecation")
    @Execution
    public void execute() {
        try {
            this.dao.findByNaturalKey(this.nome);
        } catch (BeanNotFoundException e) {
            SecurityClient securityClient = this.dao.createBean();
            securityClient.setName(this.nome);
            securityClient.getController().getSessionFactory().getCurrentSession().save(securityClient, this.databaseID);
        }
    }
}
